Questions
13 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
13 / 50

How do you select every odd or even element using pseudo-classes?

Using :nth-child() and :nth-of-type() for Odd and Even Elements

CSS pseudo-classes :nth-child() and :nth-of-type() allow you to select elements based on their position. You can use them to target every odd or even element efficiently.

How to Select Odd or Even Elements
  1. 1

    :nth-child(even) – Selects every even child of its parent (2nd, 4th, 6th, etc.).

  2. 2

    :nth-child(odd) – Selects every odd child of its parent (1st, 3rd, 5th, etc.).

  3. 3

    :nth-of-type(even) – Selects every even element of the specified type among siblings.

  4. 4

    :nth-of-type(odd) – Selects every odd element of the specified type among siblings.

Example: Selecting Odd and Even Elements

In this example, :nth-child(even) and :nth-child(odd) alternate the background color of all list items, while :nth-of-type(even) makes every even <li> bold, ignoring other sibling element types.

Best Practices
  1. 1

    Use :nth-child() when you want to style elements based on their absolute position among all children.

  2. 2

    Use :nth-of-type() when styling elements of a specific type among siblings.

  3. 3

    Combine odd/even pseudo-classes with other selectors for more precise styling.

  4. 4

    Test in complex HTML structures to ensure the desired elements are targeted correctly.

Difficulty: 3/10
Topics: nth-child selectors, odd/even element targeting, CSS specificity

Scenario Questions

0-2 years experience
  1. 1

    You're building a pricing table with alternating background colors — how would you select every odd row using CSS?

  2. 2

    A list of user comments has inconsistent styling on even items — what CSS rule would you write to fix it, and why might it not work if the HTML structure changes?

2-5 years experience
  1. 1

    Our product grid is supposed to highlight every even card, but after adding a filter that hides some items, the pattern is broken — what’s happening, and how do you fix it without changing the HTML?

  2. 2

    A designer wants every third item in a dynamic feed to have a special border, but the team is using a library that injects placeholder elements — how would you debug and solve this without touching the library code?

5-8 years experience
  1. 1

    You're designing a responsive component library where odd/even styling is used for accessibility contrast, but screen readers and dynamic content reordering break the visual pattern — how do you ensure semantic and visual consistency across devices and assistive tech?

  2. 2

    In a large-scale CMS, authors can insert arbitrary content blocks that break the nth-child pattern — how would you architect a robust CSS strategy that survives unpredictable DOM structures without relying on JS?

8+ years experience
  1. 1

    We're migrating a legacy UI from inline styles and JS-based alternation to pure CSS — how do you prioritize which components to refactor first, and how do you measure the impact on performance and accessibility across 50+ product pages?

  2. 2

    A cross-team component system uses odd/even styling inconsistently across 12 micro-frontends — how do you enforce standardization without breaking existing layouts, and what metrics would you track to validate success?

Follow-up Questions

  • What if the elements aren't direct siblings? How does that break your selector?
  • How would you debug if every other element isn't styling as expected?
  • Could you use this in a dynamic list where items are added via JavaScript?